<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<script type="text/javascript" src="<%=basePath %>js/jquery.js"></script>
<script type="text/javascript" src="<%=basePath %>js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="<%=basePath %>js/jquery.json-2.4.min.js"></script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tra/nsitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'beginExam.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<title>考试系统</title>
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/exam.css"/>
<style type="text/css">
<!--
body {
	margin-left: 3px;
	margin-top: 0px;
	margin-right: 3px;
	margin-bottom: 0px;
}
.STYLE1 {
	color: #e1e2e3;
	font-size: 14px;
}
.STYLE6 {color: #000000; font-size: 12; }
.STYLE10 {color: #000000; font-size: 12px; }
.STYLE19 {
	color: #344b50;
	font-size: 12px;
}
.STYLE21 {
	font-size: 12px;
	color: #3b6375;
}
.STYLE22 {
	font-size: 12px;
	color: #295568;
}
-->
</style>

  </head>
  
    <style>
	#div1 {width:100px;height:100pax; display:none;}
	</style>
  
  <script type="text/javascript">
  
  $(function (){
		
	})
	
  var bid;
  
  
	  Date.prototype.Format = function (fmt) { 
	    var o = {
	        "M+": this.getMonth() + 1, //月份 
	        "d+": this.getDate(), //日 
	        "h+": this.getHours(), //小时 
	        "m+": this.getMinutes(), //分 
	        "s+": this.getSeconds(), //秒 
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
	        "S": this.getMilliseconds() //毫秒 
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	}
	
  	function checkAnswer(id,type){
  		bid = id;
  		var endline="<s:property value='%{endline}'/>";
  		endline = endline.substring(0, 19);		
		var submitTime = getServerTime();

		var beginTime = submitTime;
		var endTime = endline;	
		var beginTimes = beginTime.substring(0, 10).split('-');
	    var endTimes = endTime.substring(0, 10).split('-');	
	    beginTime = beginTimes[1] + '/' + beginTimes[2] + '/' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
	    endTime = endTimes[1] + '/' + endTimes[2] + '/' + endTimes[0] + ' ' + endTime.substring(10, 19);
	    var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;
	    if (a < 0) {
	        alert("考试时间已过，无法提交");
	        return ;
	    }
  		
  		$("#"+id).attr("disabled","disabled"); 
  		var answer = $("#answer"+id).val();
  		$.ajaxSetup({ cache: false }); 
  		$.post("<%=request.getContextPath()%>/ExamAction/CheckAction!checkAnswer.action",{answer:answer,id:id,type:type}, callback,"json");
  		
  	}
  	
  	function checkTime(){
  		var endline="<s:property value='%{endline}'/>";
  		var beginline="<s:property value='%{beginline}'/>";
  		var endTime = endline.substring(0, 19);
  		var beginTime = beginline.substring(0, 19);
  		var nowTime = getServerTime();
  		var beginTimes = beginTime.substring(0, 10).split('-');
	    var endTimes = endTime.substring(0, 10).split('-');	
	    var nowTimes = nowTime.substring(0, 10).split('-');
	    
	    beginTime = beginTimes[1] + '/' + beginTimes[2] + '/' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
	    endTime = endTimes[1] + '/' + endTimes[2] + '/' + endTimes[0] + ' ' + endTime.substring(10, 19);
	   	nowTime = nowTimes[1] + '/' + nowTimes[2] + '/' + nowTimes[0] + ' ' + nowTime.substring(10, 19);
	   	var a = (Date.parse(endTime) - Date.parse(nowTime)) / 3600 / 1000;
	   	var b = (Date.parse(beginTime) - Date.parse(nowTime)) / 3600 / 1000;
	   	if(a<0||b>0){
	   		window.location.href="http://192.168.124.34:8080/acmctgu"; 
	   	}
	   	return ;
  	}
  	
  	function getServerTime(){
  		var xmlHttp = false;
		//获取服务器时间
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
			    xmlHttp = false;
			}
		}	
		if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
			xmlHttp = new XMLHttpRequest();
		}
		xmlHttp.open("GET", "null.txt", false);
		xmlHttp.setRequestHeader("Range", "bytes=-1");
		xmlHttp.send(null);
		serverTime=new Date(xmlHttp.getResponseHeader("Date")).Format("yyyy-MM-dd hh:mm:ss");
		return serverTime;
  	}
  	
  	
  	function callback(data){
  		var s = "The Score : "; 
  		var c = "Console info : "; 
  		var t = "The last sava time : ";
  		var id = data.id;
  		var info = data.result;
  		var score = data.score;
  		var time = data.time;
  	
  		if(info=="Answer Correct."){
  			$("#score"+id).text(s+score+'分');
  			$("#savatime"+id).text(t+time);
  			$("#info"+id).text(c+'正确    '+info);
  			setTimeout( function(){
  				$("#title_"+id).hide();
  			} , 5000 );
  		}
  		else{
  			$("#score"+id).css("color","red");
  			$("#score"+id).text(s+'0 分');
  			$("#savatime"+id).text(t+time).css("color","red");
  			$("#info"+id).text(c+'错误     '+info).css("color","red");
  		}
  		$("#"+bid).removeAttr("disabled");
  	}
  
  	
  
  </script>
  
  
  
  <body onload="checkTime();">
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="24" bgcolor="#353c44"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="6%" height="19" valign="bottom"><div align="center"><img src="images/tb.gif" width="14" height="14" /></div></td>
                <td width="94%" valign="bottom"><span class="STYLE1"> 考试页面 </span></td>
              </tr>
            </table>
            </td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
  <td>
    
    <br/><h3>编程题</h3><br/>
    
    <div class="style" >		
    <s:iterator value="programList" var="plist" status="status">
    
    <div id='title_<s:property value="#plist.id"/>'>
    <div class="nav">
    <s:property value="#status.index+1"/>.<s:property value="#plist.title"/> (<s:property value="#plist.score"/>分)
    </div>
    
    <div class="common">
	    <s:if test="paper.isjava == 1">
			C时间限制：<s:property value="#plist.runtime"/>&nbsp;毫秒&nbsp;|&nbsp;
			C内存限制：<s:property value="#plist.memoryforc"/>&nbsp;Kb</br>
		</s:if>
		<s:else>
			Java时间限制：<s:property value="#plist.runtimeforjava"/>&nbsp;毫秒&nbsp;|&nbsp;
			Java内存限制：<s:property value="#plist.memoryforjava"/>&nbsp;Kb	
		</s:else>	
	</div>
    
     <div class="des">
	题目内容：<br/>
	</div>
	
	<div class="cribe">
    <pre><s:property value="#plist.content"/></pre>
    </div>
    
    <div class="des">
          输入描述<br/>
    </div>
    
    <div class="cribe">
    <pre><s:property value="#plist.inputdescribe"/></pre><br/>
    </div>
    
    <div class="des">
         输出描述<br/>
    </div>
    
    <div class="cribe">
    <pre><s:property value="#plist.outputdescribe"/></pre><br/>
    </div>
    
    <div class="des">
          输入样例<br/>
    </div>
    
    <div class="cribe">
    <pre><s:property value="#plist.inputsample"/></pre><br/>
    </div>
    
    <div class="des">
         输出样例<br/>
    </div>
    
    <div class="cribe">
    <pre><s:property value="#plist.outputsample"/></pre><br/>
    </div>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <s:if test="#plist.prefix == null">

    
    
    
   <div class="des">    
        程序代码<br/>
   </div>
   
   <div class="a">
        <textarea cols="100" rows="8" id = "answer<s:property value= "#plist.id"/>"  ><s:property value= "#plist.answer"/></textarea><br/><br/>
        <input type="button" id ="<s:property value= "#plist.id"/>" value="提交" onclick="checkAnswer(<s:property value= "#plist.id"/>,<s:property value="paper.isjava"/>)"/> 
    </div>
    
    
    
    
</s:if><s:else>


<div class="des">    
        代码填空<br/>
   </div>
   
   <div class="a">
        <textarea cols="100" rows="10" id = "answer<s:property value= "#plist.id"/>"  ><s:property value= "#plist.answer"/></textarea><br/><br/>
        <input type="button" id ="<s:property value= "#plist.id"/>" value="提交" onclick="checkAnswer(<s:property value= "#plist.id"/>,<s:property value="paper.isjava"/>)"/> 
    </div>






</s:else>
    
    
    <br/>
        
      <div class="des">     
        <span id ='info<s:property value= "#plist.id"/>'></span>        <%-- 用于显示信息--%>
        &nbsp;&nbsp;<br/>
        <span id = 'savatime<s:property value= "#plist.id"/>'></span>    <%-- 用于显示保存的时间--%>
    <br/>
	    <span id= 'score<s:property value= "#plist.id"/>'></span>          <%-- 用于显示分数--%>	
	<br/><br/>
	</div>
 </div>	
    </s:iterator>
    </div>
    </td>
    </tr>
    </table>
  </body>
</html>
